package com.vitabuybuy.tv.ui.mine.comment.viewmodel

import androidx.lifecycle.MutableLiveData
import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.library.common.ext.rxHttpRequest
import com.library.common.net.error.msg
import com.library.common.toast.XToast
import com.vitabuybuy.tv.dto.MyCommentDetailDto
import com.vitabuybuy.tv.dto.MyCommentDto
import com.vitabuybuy.tv.network.MyCommentRepository

class MyCommentViewModel : BaseViewModel() {

    val datas = MutableLiveData<MutableList<MyCommentDto>>()

    fun getCommentList(pageNumber: Int) {
        rxHttpRequest {
            onRequest = {
                datas.value = MyCommentRepository.getCommentList(pageNumber, 10) .await()
                datas.value?.run {
                    defUI.msgEvent.postValue(MessageModel(1, arg1 = this.size))
                }
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = pageNumber == 1
        }
    }

    val commentDetail = MutableLiveData<MyCommentDetailDto>()
    fun getCommentDetail(id: Int?) {
        rxHttpRequest {
            onRequest = {
                commentDetail.value =MyCommentRepository.getCommentDetail(id)  .await()
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }

    fun delete(id: Int?, position: Int?) {
        rxHttpRequest {
            onRequest = {
                MyCommentRepository.getDeleteDetail(id)  .await()
                defUI.msgEvent.postValue(MessageModel(2, arg1 = position?:-1))
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }
}
